window.appModule.controller('menuControl', function ($scope,$http,$state,menuAPI,$cookies,ngNotify,$document) {
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });
    menuAPI.getUserById({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.title = result.user.nickname;
            if(result.user.nickname.length > 8){
                result.user.nickname = result.user.nickname.substring(0,8)+"...";
            }else if(result.user.nickname.length == 0){
                result.user.nickname = result.user.phoneNumber;
            }
            if('' == result.user.imageUrl){
                result.user.imageUrl = "resource/images/UserAvatar.png";
            }
            $scope.user = result.user;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    menuAPI.getMenusByUserId({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.menus = result.menuList;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    $scope.goSecondMenu = function(menuId) {
        if(1 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("robotList");
                }
            });
        }else if(2 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("knowledgeList");
                }
            });
        }
    };
    $scope.goMenu = function(menuId) {
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                $state.go("menu");
            }
        });
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block' && (event.target.id != "showUserDiv" && event.target.id != "showUserImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeUserDiv = function() {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $scope.goChangePassowrd = function() {
        $state.go("changePassword");
    };
    $scope.logOut = function() {
        menuAPI.logOut({userId:$cookies.getObject('user').id}, function(result){
            if(result.messageCode == "10000"){
                $cookies.putObject('user', "");
                $state.go("loginQR");
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
});
